package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.ejia.dao.BaseDao;
import com.ejia.pojo.house;
import com.ejia.pojo.user;
import com.ejia.server.HouseService;
import com.ejia.server.UserService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;

public class YZXXGLADD extends JFrame {

	private JPanel contentPane;
	private JTextField txtname;
	private JTextField txtuid;
	private JTextField txtpwd;
	private JTextField txtphone;
	private JTextField txtmph;
	private static ArrayList<house> houses;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					YZXXGLADD frame = new YZXXGLADD();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public YZXXGLADD() {
		setTitle("业主信息管理添加");
		setIconImage(Toolkit.getDefaultToolkit().getImage(YZXXGLADD.class.getResource("/com/ejia/photo/客户管理系统.png")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 645, 868);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("添加信息");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("宋体", Font.PLAIN, 42));
		label.setBounds(202, 33, 218, 65);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("业主姓名");
		label_1.setFont(new Font("宋体", Font.PLAIN, 32));
		label_1.setBounds(74, 113, 158, 47);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("业主账号");
		label_2.setFont(new Font("宋体", Font.PLAIN, 32));
		label_2.setBounds(74, 200, 158, 47);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("密码");
		label_3.setFont(new Font("宋体", Font.PLAIN, 32));
		label_3.setBounds(74, 287, 158, 47);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("性别");
		label_4.setFont(new Font("宋体", Font.PLAIN, 32));
		label_4.setBounds(74, 374, 158, 47);
		contentPane.add(label_4);
		
		JLabel label_5 = new JLabel("电话");
		label_5.setFont(new Font("宋体", Font.PLAIN, 32));
		label_5.setBounds(74, 461, 158, 47);
		contentPane.add(label_5);
		
		JLabel label_6 = new JLabel("小区");
		label_6.setFont(new Font("宋体", Font.PLAIN, 32));
		label_6.setBounds(74, 548, 158, 47);
		contentPane.add(label_6);
		
		JLabel label_7 = new JLabel("门牌号");
		label_7.setFont(new Font("宋体", Font.PLAIN, 32));
		label_7.setBounds(74, 635, 158, 47);
		contentPane.add(label_7);
		
		txtname = new JTextField();
		txtname.setFont(new Font("宋体", Font.PLAIN, 24));
		txtname.setBounds(306, 123, 241, 27);
		contentPane.add(txtname);
		txtname.setColumns(10);
		
		txtuid = new JTextField();
		txtuid.setFont(new Font("宋体", Font.PLAIN, 24));
		txtuid.setColumns(10);
		txtuid.setBounds(306, 210, 241, 27);
		contentPane.add(txtuid);
		
		txtpwd = new JTextField();
		txtpwd.setFont(new Font("宋体", Font.PLAIN, 24));
		txtpwd.setColumns(10);
		txtpwd.setBounds(306, 297, 241, 27);
		contentPane.add(txtpwd);
		
		txtphone = new JTextField();
		txtphone.setFont(new Font("宋体", Font.PLAIN, 24));
		txtphone.setColumns(10);
		txtphone.setBounds(306, 471, 241, 27);
		contentPane.add(txtphone);
		
		txtmph = new JTextField();
		txtmph.setFont(new Font("宋体", Font.PLAIN, 24));
		txtmph.setColumns(10);
		txtmph.setBounds(306, 645, 241, 27);
		contentPane.add(txtmph);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setFont(new Font("宋体", Font.PLAIN, 24));
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"男", "女"}));
		comboBox.setBounds(306, 384, 241, 27);
		contentPane.add(comboBox);
		
		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setFont(new Font("宋体", Font.PLAIN, 24));
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"丽景豪庭", "时代名都", "黄金国际", "中瑞华侨城"}));
		comboBox_1.setBounds(306, 558, 241, 27);
		contentPane.add(comboBox_1);
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name=txtname.getText();
				String uid=txtuid.getText();
				String pwd=txtpwd.getText();
				String phone=txtphone.getText();
				String mph=txtmph.getText();
				int parentid=0;
				if (comboBox_1.getSelectedItem().equals("丽景豪庭")) {
					parentid=1;
				}else if (comboBox_1.getSelectedItem().equals("时代名都")) {
					parentid=2;
				}else if (comboBox_1.getSelectedItem().equals("黄金国际")) {
					parentid=3;
				}else if (comboBox_1.getSelectedItem().equals("中瑞华侨城")) {
					parentid=4;
				}
				if (HouseService.yzxxadd(mph, parentid)  ) {
					houses=HouseService.find(mph);
					int hid=houses.get(0).getHid();
					if (UserService.yzxxgltj(uid, pwd, name,(String) comboBox.getSelectedItem(), phone, hid)) {
						int id=UserService.find(name).get(0).getId();
						if (BaseDao.execute("insert into tb_car(carno,carnumber,userid,statu) values(\" \",\" \",?,\" \")", id)) {
							JOptionPane.showMessageDialog(null, "添加成功");
							new YZXXGL().setVisible(true);
							dispose();
						}
					}else {
						JOptionPane.showMessageDialog(null, "添加失败");
					}
				}else {
					JOptionPane.showMessageDialog(null, "房号输入错误");
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 32));
		button.setBounds(125, 714, 123, 70);
		contentPane.add(button);
		
		JButton button_1 = new JButton("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new YZXXGL().setVisible(true);
				dispose();
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 32));
		button_1.setBounds(373, 714, 123, 70);
		contentPane.add(button_1);
		setLocationRelativeTo(null);
	}
}
